<?php

namespace app\admin\validate;

use think\Validate;

class AdminValidate extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
	protected $rule = [
        "account"     => "require|unique:admin",
        "password"    => "require|checkPwd",
    ];

    /**
     * 登录密码格式验证
     */
    public function checkPwd($param){
        //密码判断 数字、字母或下滑线   3-20位以内
        if(preg_match("/[\w]{3,20}/", $param)){
            return true;
        }
        return false;
    }

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */
    protected $message = [
        "account.require"    => '请输入账号',
        "account.unique"     => '该用户名已存在',
        "password.require"   => '请输入密码',
        "password.checkPwd"  => '密码格式不正确',
    ];


    //定义应用场景

    protected $scene = [
       //'login' => ['account' => 'require', 'password' => 'require'],
    ];


    //登录场景
    public function sceneLogin(){
        return $this->only(['account','password'])
            ->remove('account', 'unique')
            ->remove('password', 'checkPwd');
    }


    //修改场景
    public function sceneUpdate(){
        return $this->only(['password'])
            ->remove('password', 'require');
    }

}
